package model.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import model.dao.AccountDAO;
import model.dao.AcountTypeDAO;
import model.pojo.Account;
import model.pojo.AccountType;
import model.util.UtilInitLang;

@ManagedBean(name="accountBean")
@SessionScoped
public class AccountBean {
	
	
//	@ManagedProperty(value = "#{utilInitLang.getClassEntity()}")
//	private UtilInitLang utilInitLang;
	
	private String nAccount;
	private String nDescrVN;
	private String nDescrEN;
	private String nDescrKR;
	private String nAccountType;
	private String nClassID;
	private String nStatus;
	private Account account = new Account();
	AccountDAO accountDAO = new AccountDAO();
	AcountTypeDAO acountTypeDAO = new AcountTypeDAO();
	private List<Account> lstAccount = new ArrayList<Account>();
	private List<String> lstAccountType = new ArrayList<String>();
	
//	public UtilInitLang getUtilInitLang() {
//		return utilInitLang;
//	}
//	public void setUtilInitLang(UtilInitLang utilInitLang) {
//		this.utilInitLang = utilInitLang;
//	}
	public String getnAccount() {
		return nAccount;
	}
	public void setnAccount(String nAccount) {
		this.nAccount = nAccount;
	}
	public String getnDescrVN() {
		return nDescrVN;
	}
	public void setnDescrVN(String nDescrVN) {
		this.nDescrVN = nDescrVN;
	}
	public String getnDescrEN() {
		return nDescrEN;
	}
	public void setnDescrEN(String nDescrEN) {
		this.nDescrEN = nDescrEN;
	}
	public String getnDescrKR() {
		return nDescrKR;
	}
	public void setnDescrKR(String nDescrKR) {
		this.nDescrKR = nDescrKR;
	}
	public String getnAccountType() {
		return nAccountType;
	}
	public void setnAccountType(String nAccountType) {
		this.nAccountType = nAccountType;
	}
	
	public String getnStatus() {
		return nStatus;
	}
	public void setnStatus(String nStatus) {
		this.nStatus = nStatus;
	}
	public void setAccount(Account account) {
		this.account = account;
	}
	public Account getAccount() {
		return account;
	}
	public void setnClassID(String nClassID) {
		this.nClassID = nClassID;
	}
	public String getnClassID() {
		return nClassID;
	}
	
	private void setEmptyInput(){
		setnAccount("");
		setnDescrEN("");
		setnDescrVN("");
		setnDescrKR("");
		setnAccountType("");
		setnClassID("");
		setnStatus("");
	}
	public void setLstAccount(List<Account> lstAccount) {
		this.lstAccount = lstAccount;
	}
	public List<Account> getLstAccount() {
		return getAllAccount();
	}
	
	public List<Account> getAllAccount(){
		return accountDAO.getAllAccount();
	}
	public void setLstAccountType(List<String> lstAccountType) {
		this.lstAccountType = lstAccountType;
	}
	public List<String> getLstAccountType() {
		return getAllAccountType();
	}
	public List<String> getAllAccountType(){
		return acountTypeDAO.getAllAccountType();
	}

	// insert account
	public void insertAccount(ActionEvent actionEvent){
		account.setAccount(getnAccount());
		account.setDescrVN(getnDescrVN());
		account.setDescrEN(getnDescrEN());
		account.setDescrKR(getnDescrKR());
		account.setAccountType(getnAccountType());
		account.setClassID(getnClassID());
		account.setStatus(getnStatus());
		account.setGa(true);
		
		int x = accountDAO.saveAccount(account);
		if(x>0){
			System.out.println("insert successfuly!");
			setEmptyInput();
		}
		else{
			System.out.println("Insert unsuccessfuly!");
		}
		lstAccount = null;
	}
	// new account
	public void newAccount(ActionEvent actionEvent){
		setEmptyInput();
	}
	// delete account
	public void deleteAccount(ActionEvent actionEvent){
		int x = accountDAO.deleteAccount(getnAccount());
		if(x>0){
			System.out.println("delete successfuly!");
			setEmptyInput();
		}else{
			System.out.println("Delete unSuccessfuly");
		}
		lstAccount = null;
	}
	// edit account
	public void editAccount(ActionEvent actionEvent){
		account.setAccount(getnAccount());
		account.setDescrVN(getnDescrVN());
		account.setDescrEN(getnDescrEN());
		account.setDescrKR(getnDescrKR());
		account.setAccountType(getnAccountType());
		account.setClassID(getnClassID());
		account.setStatus(getnStatus());
		
		int x = accountDAO.updateAccount(account);
		if(x>0){
			System.out.println("update successfuly!");
			setEmptyInput();
		}
		else{
			System.out.println("update unsuccessfuly!");
		}
		lstAccount = null;
	}
	
	
}
